import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:zhaoxiansheng_app/controller/LoginController.dart';
import 'package:zhaoxiansheng_app/model/Person.dart';
import 'package:image_picker/image_picker.dart';

class LoginPage extends StatelessWidget {
  final TextEditingController _userNameController = TextEditingController();
  final TextEditingController _userPasswordController = TextEditingController();
  final loginController = Get.find<LoginController>();
  var storage = GetStorage();
  final picker = ImagePicker();
  Rx<String>imageUrl = Rx("");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Colors.blueAccent.shade200,
                  Colors.purpleAccent.shade100,
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            child: Center(
              child: Card(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20),
                ),
                elevation: 8,
                color: Colors.white,
                child: Padding(
                  padding: const EdgeInsets.all(30),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "登录",
                        style: TextStyle(
                          fontSize: 28,
                          fontWeight: FontWeight.bold,
                          color: Colors.deepPurple,
                        ),
                      ),
                      const SizedBox(height: 30),
                      TextField(
                        controller: _userNameController,
                        decoration: InputDecoration(
                          prefixIcon: Icon(
                            Icons.person,
                            color: Colors.deepPurple,
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          labelText: "用户名",
                        ),
                      ),
                      const SizedBox(height: 20),
                      TextField(
                        controller: _userPasswordController,
                        obscureText: true,
                        decoration: InputDecoration(
                          prefixIcon: Icon(
                            Icons.lock,
                            color: Colors.deepPurple,
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          labelText: "密码",
                        ),
                      ),
                      SizedBox(height: 20),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          FilledButton(
                            onPressed: () async {
                              var image = await picker.pickImage(
                                source: ImageSource.gallery,
                              );
                              if (image != null) {
                                imageUrl.value = image.path;
                                Get.snackbar("上传图片成功","");
                              } else {
                                Get.snackbar("上传图片失败", "请重新上传图片");
                              }
                            },
                            child: Text("点击上传头像"),
                          ),
                          Obx((){
                            if(imageUrl.value!= ""){
                              return CircleAvatar(backgroundImage: FileImage(File(imageUrl.value)));
                            }
                            else{
                              return Text("您还没有选择图片");
                            }
                          })
                        ],
                      ),

                      const SizedBox(height: 30),
                      ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Colors.deepPurple,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          padding: const EdgeInsets.symmetric(
                            horizontal: 50,
                            vertical: 15,
                          ),
                        ),
                        onPressed: () {
                          var userName = _userNameController.text;
                          if (userName.isEmpty) {
                            Get.dialog(
                              AlertDialog(
                                title: Text("提示"),
                                content: Text("用户名不能为空！"),
                                actions: [
                                  TextButton(
                                    onPressed: () {
                                      Get.back(); // 关闭对话框
                                    },
                                    child: Text("确定"),
                                  ),
                                ],
                              ),
                            );
                            return; // 阻止继续执行登录逻辑
                          }
                          var userPassword = _userPasswordController.text;
                          loginController.login(userName);
                          //持久化用户信息
                          Person p = new Person(
                            userName: userName,
                            userPassword: userPassword,
                            imageUrl: imageUrl.value
                          );
                          storage.write("userInfo", p.toMap());
                          print("已将userInfo存到storage");
                          Get.snackbar(
                            "恭喜您登录成功",
                            "${_userNameController.text}",
                          );
                          Future.delayed(Duration(seconds: 2), () {
                            Get.toNamed("/");
                          });
                        },
                        child: Text(
                          "登录",
                          style: TextStyle(fontSize: 16, color: Colors.white),
                        ),
                      ),
                      const SizedBox(height: 20),
                      Text(
                        "非常感谢您注册该APP的账号",
                        style: TextStyle(
                          color: Colors.deepPurple.shade400,
                          fontSize: 14,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          Positioned(
            child: GestureDetector(
              child: Icon(Icons.chevron_left, size: 40),
              onTap: () {
                Get.back();
              },
            ),
            left: 5,
            top: 30,
          ),
        ],
      ),
    );
  }
}
